<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Db;
use think\Controller;

class Goods extends Base
{
    public function index()
    {
        $data = input('param.');
        $query = http_build_query($data);
        //halt($data);

        $whereData = [];


        if(!empty($data['name'])) {
            $whereData['name'] = ['like', '%'.$data['name'].'%'];
        }
        if(!empty($data['cate_id'])) {
            $whereData['cate_id'] = intval($data['cate_id']);
        }
        $this->getPageAndSize($data);

        // 获取表里面的数据
        $goods = model('goods')->getGoodsByCondition($whereData, $this->from, $this->size);
        // 获取满足条件的数据总数 =》 有多少页

        $total = model('goods')->getGoodsCountByCondition($whereData);
        /// 结合总数+size  =》 有多少页
        $pageTotal = ceil($total/$this->size);//1.1 =>2


        $category=model('Category')->select();
        return $this->fetch('',['goods'=> $goods,
            'pageTotal' => $pageTotal,
            'total'=>$total,
            'curr' => $this->page,
            'query' => $query,
            'category'=>$category
        ]);
    }
        public function add()
        {
            $category=model('Category')->select();
            return $this->fetch('',['category'=>$category]);
        }

        public function save()
        {
            if(request()->isPost()) {

                $data = input('post.');
                $type =$_FILES['face']['type'];
                $file = request()->file('face');
                if (empty($file)) {
                    //必须上传商品logo图片
                    $this->fail('请上传文件');
                }
                //将文件移动到指定的目录（public 目录下  uploads目录 goods目录）
                $dir = ROOT_PATH . 'public' . DS . 'uploads' . DS . $type;
                if(!is_dir($dir)) mkdir($dir,0777,true);;
                $info = $file->validate(['size' => 10*1024*1024, 'ext' => ['jpg', 'png', 'gif', 'jpeg']])->move($dir);
                if (empty($info)) {
                    //上传出错
                    $msg = $file->getError();
                    $this->fail($msg);
                }
                //拼接图片的访问路径
                $logo = DS . "uploads" . DS . $type . DS . $info->getSaveName();

                $insertData=[
                    'name'=>$data['name'],
                    'cate_id'=>$data['cate_id'],
                    'price'=>$data['price'],
                    'stock'=>$data['stock'],
                    'parameter'=>$data['parameter'],
                    'headimg'=>$logo,
                    'detail'=>$data['editorValue'],
                    'displayorder'=>$data['displayorder']
                ];

                //入库操作
                $id = model('goods')->create($insertData, true);;

                if($id) {
                    $this->success('新增成功', 'index');
                } else {
                    $this->error('新增失败', 'index');
                }



            }
        }
        public function del($id)
        {
            $goods =model('goods')::find($id);
            $goods->delete();


            $this->success('删除成功', 'index');
        }
    public function stop($id)
    {
        $goods =model('goods')::find($id);
        if($goods['status']==1){
            Db::table('fx_goods')
                ->where('id', $id)
                ->update(['status' => 0]);
            $this->success('停用成功', 'index');
        }else{
            Db::table('fx_goods')
                ->where('id', $id)
                ->update(['status' => 1]);
            $this->success('启用成功', 'index');
        }

    }
    public function edit($id)
    {
        $goods =model('goods')::find($id);
        $category=model('Category')->select();


        return $this->fetch('edit',['goods'=>$goods,
            'category'=> $category
            ]);
    }

    public function update()
    {
        $data = input('post.');
        if($_FILES['face']['tmp_name'])
        {
            $type =$_FILES['face']['type'];
            $file = request()->file('face');
            if (empty($file)) {
                //必须上传商品logo图片
                $this->fail('请上传文件');
            }
            //将文件移动到指定的目录（public 目录下  uploads目录 goods目录）
            $dir = ROOT_PATH . 'public' . DS . 'uploads' . DS . $type;
            if(!is_dir($dir)) mkdir($dir,0777,true);;
            $info = $file->validate(['size' => 10*1024*1024, 'ext' => ['jpg', 'png', 'gif', 'jpeg']])->move($dir);
            if (empty($info)) {
                //上传出错
                $msg = $file->getError();
                $this->fail($msg);
            }
            //拼接图片的访问路径
            $logo = DS . "uploads" . DS . $type . DS . $info->getSaveName();
            $insertData=[
                'name'=>$data['name'],
                'cate_id'=>$data['cate_id'],
                'price'=>$data['price'],
                'stock'=>$data['stock'],
                'parameter'=>$data['parameter'],
                'headimg'=>$logo,
                'detail'=>$data['editorValue'],
                'displayorder'=>$data['displayorder']
            ];
            model('goods')::update($insertData,['id' => $data['id']], true);
        }else{
            model('goods')::update($data,['id' => $data['id']], true);
        }


        $this->success('修改成功', 'index');
    }
    
   
}